﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using SchoolProject2012.Areas.Admin.Models;

namespace SchoolProject2012.Functions
{
    public class Security
    {
        public static string getHash(string text)
        {
            byte[] data = Encoding.Default.GetBytes(text);
            HMACSHA256 hhmac = new HMACSHA256(data);
            data = hhmac.ComputeHash(data);
            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // Return the hexadecimal string.
            text = sBuilder.ToString();
            return text;
        }

        public static bool verifHash(string text, string hash)
        {
            bool rep = false;
            if (getHash(text).CompareTo(hash) == 0)
            {
                rep = true;
            }
            return rep;
        }

        public static void exporter(SchoolProjectBddEntities2 db)
        {
            try
            {
                if (Directory.Exists(@"C:\Users\STEVIX\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\App_Data"))
                {
                    if (File.Exists(@"C:\Users\STEVIX\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\App_Data\users.bak"))
                    {
                        File.Delete(@"C:\Users\STEVIX\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\App_Data\users.bak");
                    }
                    db.ExecuteStoreCommand(@"backup database SchoolProjectBdd to disk ='C:\Users\STEVIX\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\App_Data\users.bak' with init,stats=10");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}